//
//  ZuijinSelectView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/2.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ZuijinSelectView.h"
#import "FujinCell.h"

#import "IslanOrder_typeModel.h"

#define CellHight 40

@interface ZuijinSelectView ()<UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableview;

@end

@implementation ZuijinSelectView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = TouMingBlack;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selfTap)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];

    }
    return self;
}

-(void)selfTap{

    [self.deleget ZuijinAlphaIsCellClick:NO WhithSting:nil];
    self.alpha = 0;
}

-(void)setOrderTypeAry:(NSArray *)orderTypeAry{

    _orderTypeAry = orderTypeAry;

    [self CreatBacView];

}

/**
 *  点击backView 将键盘收起
 */
-(void)backViewTap{
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

-(void)CreatBacView{
    
    
    [self.tableview removeFromSuperview];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CellHight*self.orderTypeAry.count) style:UITableViewStylePlain];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.scrollEnabled = NO;
    [self.tableview registerClass:[FujinCell class] forCellReuseIdentifier:@"fujinCell"];
    [self addSubview:self.tableview];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.orderTypeAry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FujinCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"fujinCell"];
    
    IslanOrder_typeModel *model = self.orderTypeAry[indexPath.row];
    
    cell.text.text = model.name;
    tableView.separatorStyle  = 0;
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    
    if (indexPath.row==0) {
        cell.lin.backgroundColor = mainColor;
        cell.text.textColor = mainColor;
    }

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
     FujinCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    UIView * allcellView = tableView.subviews[0];
    for (FujinCell * otherCell in allcellView.subviews) {
        
        if ([otherCell isKindOfClass:[FujinCell class]]) {
            otherCell.text.textColor = [UIColor blackColor];
            otherCell.lin.backgroundColor = [UIColor lightGrayColor];
        }
    }
    
    cell.lin.backgroundColor = mainColor;
    cell.text.textColor = mainColor;
    
    
    [self.deleget ZuijinAlphaIsCellClick:YES WhithSting:cell.text.text];
     self.alpha = 0;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CellHight;
}


/**
 *  对某些手势进行 禁用
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        
        return NO;
    }
    
    return YES;
}

@end
